import { useDistrict } from "~/store/modules/area";


export const publicExpandCardStepToText = () => {
	return "挂牌期";
};



let district: null | ReturnType<typeof useDistrict> = null;

/**
 * 根据id转换为可读的行政区文本格式
 * @param cityId 区域id
 * @returns 行政区可读文本
 */
export const convertCityName = (
	cityId: number | string
) => {	
	if (district === null) {
		district = useDistrict();
	}

	if (cityId === "330000") {
		return {
			"districtName": "浙江省",
			"districtCode": "330000"
		};
	}

	const rs = district.idMapToDistrict[cityId.toString()];

	return rs;
};

let dom = null;

export function parserStringIntoHtml(htmlString: string, repeat = 3): HTMLElement | string {
	try {
		if (repeat === 0) {
			return htmlString;
		}
		if (dom === null) {
			dom = new DOMParser();
		}

		let doc = dom.parseFromString(htmlString, "text/html");
		doc = dom.parseFromString(
			"<div>" + doc.body.innerText + "</div>",
			"text/html"
		);

		const firstChild = doc.body.firstElementChild;

		if (firstChild.firstElementChild === null) {
			return parserStringIntoHtml(firstChild.firstChild, repeat - 1);
		}

		return firstChild;
	} catch (exx) {
		console.log(exx);
		return null;
	}
}

// 反编译html
export const htmlDecode = (text: string) => {
	const reg = /<[^>]+>/g;
	if (reg.test(text)) {
		return text;
	}
	let temp = document.createElement("div");
	temp.innerHTML = text;
	const output = temp.innerText || temp.textContent;
	temp = null;
	return output;
};

// 判断是否为空
export function isEmpty(o) {
	return typeof o === "undefined" || o === "" || o === null ? "-" : o;
}
